﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Navigation;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.AutoMapper;
using Abp.Web.Models;
using KelonProud.Administration.Domain.Common;
using KelonProud.Administration.Dtos;
using KelonProud.Administration.Managers.Application;
using KelonProud.Administration.Managers.Authorization;
using KelonProud.Administration.Managers.MultiTenancy;
using KelonProud.Administration.Services;
using KelonProud.Mvc;
using Microsoft.AspNetCore.Mvc;

namespace KelonProud.Administration.Controllers
{
    [AbpMvcAuthorize]
    [Route("admin/[controller]/[action]")]
    public class NavigationController: KelonProudControllerBase
    {
        private readonly TenantManager _tenantManager;
        private readonly RoleManager _roleManager;
        private readonly EditionManager _editionManager;
        private readonly NavigationManager _navigationManager;
        private readonly INavigationProvider _navigationProvider;

        public NavigationController(TenantManager tenantManager, RoleManager roleManager, EditionManager editionManager, NavigationManager navigationManager, INavigationProvider navigationProvider)
        {
            _tenantManager = tenantManager;
            _roleManager = roleManager;
            _editionManager = editionManager;
            _navigationManager = navigationManager;
            _navigationProvider = navigationProvider;
        }

        public ActionResult Index()
        {
            return View("KelonProud.Administration.Views.Navigation.Index");
        }

        public ActionResult Create()
        {
            var tenants = _tenantManager.Tenants.ToList().Select(s => s.MapTo<TenantOutputDto>()).ToList();
            tenants.Insert(0, new TenantOutputDto { Id = -1, Name = "---无---" });

            var model = new NavigationInputDto
            {
                RoleDtos = _roleManager.Roles.ToList().Select(s => s.MapTo<RoleOutPutDto>()),
                Editions = _editionManager.Editions.ToList().Select(s => s.MapTo<EditionOutput>()),
                Tenants = tenants
            };

            return View("KelonProud.Administration.Views.Navigation.Edit", model);
        }

        public ActionResult Edit(int id)
        {
            var nav = _navigationManager.GetById(id);

            if (nav == null)
                throw new Exception("需要编辑的菜单不存在!");

            var model = nav.MapTo<NavigationInputDto>();

            var tenants = _tenantManager.Tenants.ToList().Select(s => s.MapTo<TenantOutputDto>()).ToList();
            tenants.Insert(0, new TenantOutputDto { Id = -1, Name = "---无---" });

            model.RoleDtos = _roleManager.Roles.ToList().Select(s => s.MapTo<RoleOutPutDto>());
            model.Editions = _editionManager.Editions.ToList().Select(s => s.MapTo<EditionOutput>());
            model.Tenants = tenants;
            model.RoleIds = nav.Roles.Select(s => s.RoleId.ToString()).ToArray();
            model.EditionIds = nav.Editions.Select(s => s.EditionId.ToString()).ToArray();

            return View("KelonProud.Administration.Views.Navigation.Edit",model);
        }

        [HttpPost]
        [WrapResult(WrapOnSuccess = false)]
        public Task<IEnumerable<MenuItem>> GetUserTopNavigations(string searchStr)
        {
            return _navigationProvider.GetUserTopNavigations(searchStr);
        }
    }
}
